//
//  DynamicColumnFieldCore+Internal.m
//  
//
//  Created by wjxfhxy on 15/12/30.
//
//

#import "DynamicColumnFieldCore+Internal.h"

NSString *const ImagePanelThumbDowdloadFolder = @"DImageThumb";

@implementation DynamicColumnFieldCore (Internal)

- (id)valueForFieldDescript:(FieldDescript *)fieldDescript
{
    id packObj = [_dataSource packObjOfDynamicColumnFieldCore:self];
    
    if(packObj == nil)
        return nil;
    
    id value = nil;
    
    XMSQueueDictionary *transformMap = [_dataSource transformMapOfDynamicColumnFieldCore:self];
    
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
    {
        NSString *key = [transformMap getValueForKey:fieldDescript.fieldName];
        if(key.length > 0)
            value = [packObj valueForKey:key];
    }
    else
    {
        value = [packObj expandfields][fieldDescript.fieldName];
    }
    return value;
}

- (id)formatValue:(id)value fieldDescript:(FieldDescript*)fieldDescript
{
    if(value == nil && [value isKindOfClass:[NSNull class]] == YES)
        return nil;
    
    BOOL operated = NO;
    if(fieldDescript.controlType.intValue == DynamicFieldControlTypePosition)
    {
        operated = YES;
        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length > 0)
        {
            NSArray *ar = [value componentsSeparatedByString:@";"];
            value = ar.count == 3 ? ar[2]: value;
        }
    }
    else if(fieldDescript.controlType.intValue == DynamicFieldControlTypeMultiSelect)
    {
        operated = YES;
        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length > 0)
        {
            NSArray *ar = [value componentsSeparatedByString:SplitCommaString];
            NSString *text = [NSString stringWithFormat:@"%@", ar[0]];
            if (ar.count > 1 && ar.count < fieldDescript.optionals.count)
                text = [NSString stringWithFormat:@"%@等%@个选项", ar[0], @(ar.count)];
            else if (ar.count == fieldDescript.optionals.count)
                text = @"全部";
            value = text;
        }
    }
    else if(fieldDescript.controlType.intValue == DynamicFieldControlTypeDateTime )
    {
        operated = YES;
        if([value isKindOfClass:[NSDate class]])
        {
            value = [(NSDate*)value toStringDate:NSDateFormatByyMdHm];
        }
    }
    
    id tmp = value;
    if([_dataSource respondsToSelector:@selector(dynamicColumnFieldCore:formatDynamicColumnCellValue:fieldDescript:)])
        tmp = [_dataSource dynamicColumnFieldCore:self formatDynamicColumnCellValue:value fieldDescript:fieldDescript];
    if(tmp == value && operated == NO)
    {
        if([value isKindOfClass:[NSString class]])
        {
            ;
        }
        else if([value isMemberOfClass:[XMSNumberInt class]] || [value isMemberOfClass:[XMSNumberLongLong class]] )
        {
            value = [(XMSNumber*)value toNSString];
        }
        else if([value isMemberOfClass:[XMSNumberDouble class]] || [value isMemberOfClass:[XMSNumberFloat class]])
        {
            NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:4 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
            NSDecimalNumber *d = [NSDecimalNumber decimalNumberWithString:[value toNSString]];
            d = [d decimalNumberByRoundingAccordingToBehavior:handler];
            value = [NSString stringWithFormat:@"%@",d];
        }
        else if([value isKindOfClass:[NSDate class]])
        {
            value = [(NSDate*)value toStringDate:NSDateFormatByyMd];
        }
        else if(([value isKindOfClass:[NSNumber class]]))
        {
            if(memcmp(((NSNumber*)value).objCType , "i", 1) == 0 || memcmp(((NSNumber*)value).objCType , "q", 1) == 0)
            {
                value = [value stringValue];
            }
            else if(memcmp(((NSNumber*)value).objCType , "d", 1) == 0)
            {
                NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:4 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
                NSDecimalNumber *d = [NSDecimalNumber decimalNumberWithString:[value stringValue]];
                d = [d decimalNumberByRoundingAccordingToBehavior:handler];
                value = [NSString stringWithFormat:@"%@",d];
            }
        }
        else
        {
            value = @"";
        }
    }
    else
    {
        value = tmp;
    }
    return value;
}

@end
